package bb.lanxing.devices.utils;

import android.content.Context;
import android.util.Log;

import bb.lanxing.devices.config.AppDeviceConfiguration;
import bb.lanxing.devices.config.AppDeviceLogger;
import bb.lanxing.devices.config.AppDeviceProvider;
import bb.lanxing.devices.config.SimpleDeviceServiceBinder;
import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.DeviceContext;
import bb.lanxing.lib.devices.bici.BiciController;
import bb.lanxing.lib.devices.ble.battery.BatteryController;
import bb.lanxing.lib.devices.ble.heartrate.HeartRateBeltController;
import bb.lanxing.lib.devices.ble.light.LightDeviceController;
import bb.lanxing.lib.devices.ble.switchablecadence.SwitchableCadenceController;
import bb.lanxing.lib.devices.cad01.Cadence01Controller;
import bb.lanxing.lib.devices.core.ble.AbsBleDevice;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;

import bb.lanxing.lib.devices.heartrate01.HeartRate01Controller;
import bb.lanxing.lib.devices.sprint.XossDeviceController;
import bb.lanxing.lib.devices.utils.DeviceManagerHelper;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.model.database.Device;
import bb.lanxing.util.text.TextUtils;

public class XZDeviceHelper {
    public static final String KEY_LAST_DEVICE_ADDRESS = "key_last_device_address_";

    private static boolean isInit = false;
    private static DeviceConnectionStateNotifier mStateNotifier;

    public static DeviceContext init(Context context) {
        if (!isInit) {
            isInit = true;
            return DeviceContext.init(context)
                    .setDeviceProvider(new AppDeviceProvider())
                    .setDeviceConfiguration(new AppDeviceConfiguration())
                    .setDeviceLogger(new AppDeviceLogger())
                    .setDeviceServiceBinder(new SimpleDeviceServiceBinder());
        }
        return DeviceContext.init(context);
    }

    public static void onReady(Context context) {
        if (DeviceContext.isReleased()) {
            init(context);
            if (mStateNotifier == null) {
                mStateNotifier = new DeviceConnectionStateNotifier();
            }
            DeviceContext.addBindTask(() -> {
                DeviceManager deviceManager = DeviceContext.getDeviceManager();
                if (deviceManager != null) {
                    deviceManager.registerConnectionStateListener(XZDeviceHelper.mStateNotifier);
                }
            });
        }
        DeviceContext.onReady(context);
    }

    public static void release() {
        DeviceManager deviceManager = DeviceContext.getDeviceManager();
        DeviceConnectionStateNotifier deviceConnectionStateNotifier = mStateNotifier;
        if (deviceConnectionStateNotifier != null) {
            if (deviceManager != null) {
                deviceManager.unregisterConnectionStateListener(deviceConnectionStateNotifier);
            }
            mStateNotifier.removeAllListeners();
            mStateNotifier = null;
        }
        DeviceContext.release();
        isInit = false;
    }

    public static void registerConnectionStateListener(ConnectionListener connectionListener) {
        if (mStateNotifier == null) {
            mStateNotifier = new DeviceConnectionStateNotifier();
        }
        mStateNotifier.registerConnectionListener(connectionListener);
    }

    public static void unregisterConnectionStateListener(ConnectionListener connectionListener) {
        DeviceConnectionStateNotifier deviceConnectionStateNotifier = mStateNotifier;
        if (deviceConnectionStateNotifier != null) {
            deviceConnectionStateNotifier.unregisterConnectionListener(connectionListener);
        }
    }

    public static boolean isConnected(int deviceType) {
        return DeviceHelper.isConnected(deviceType);
    }

    public static boolean isConnected(String deviceAddress) {
        return DeviceHelper.isConnected(deviceAddress);
    }

    public static boolean isConnecting(String deviceAddress) {
        return DeviceHelper.isConnecting(deviceAddress);
    }

    public static void disconnect(int deviceType) {
        DeviceHelper.disconnect(deviceType);
    }

    public static void disconnect(String deviceAddress) {
        DeviceHelper.disconnect(deviceAddress);
    }

    public static void connect(SmartDevice smartDevice) {
        DeviceHelper.connect(smartDevice);
    }

    public static DeviceManager getDeviceManager() {
        return DeviceHelper.getDeviceManager();
    }

    public static HeartRateBeltController getHeartRateController() {
        return DeviceManagerHelper.getHeartRateBeltController();
    }

    public static SwitchableCadenceController getSwitchableCadenceController() {
        return DeviceManagerHelper.getSwitchableCadenceController();
    }

    public static Cadence01Controller getCadence01Controller() {
        return DeviceManagerHelper.getCadence01Controller();
    }

    public static HeartRate01Controller getHeartRate01Controller() {
        return DeviceManagerHelper.getHeartRate01Controller();
    }

    public static XossDeviceController getSprintController(String str) {
        return DeviceManagerHelper.getXossDeviceController(str);
    }

    public static BiciController getBiciController() {
        return DeviceManagerHelper.getBiciController();
    }

    public static LightDeviceController getLightDeviceController(String str) {
        return DeviceManagerHelper.getLightDeviceController(str);
    }

    public static BatteryController getBatteryController(String str) {
        return DeviceManagerHelper.getBatteryController(str);
    }

    public static String getLastDeviceAddress(int i) {
        return SharedManager.getInstance().getString(KEY_LAST_DEVICE_ADDRESS + i, null);
    }

    public static void setLastDeviceAddress(int i, String str) {
        if (TextUtils.isEmptyOrNull(str)) {
            return;
        }
        SharedManager.getInstance().setValue(KEY_LAST_DEVICE_ADDRESS + i, str);
    }

    public static boolean hasBoundedDevices() {
        return Device.hasBoundedDevices();
    }

    public static String getVersion(int i) {
        if (i != 6) {
            return null;
        }
        return SharedManager.getInstance().getString("key_qi_version", null);
    }

    public static void setVersion(int i, String str) {
        if (i != 6) {
            return;
        }
        SharedManager.getInstance().setValue("key_qi_version", str);
    }

    public static String getFirmwarePath(int i) {
        if (i != 6) {
            return null;
        }
        return SharedManager.getInstance().getString("key_qi_fw_path", null);
    }

    public static void setFirmwarePath(int i, String str) {
        if (i != 6) {
            return;
        }
        SharedManager.getInstance().setValue("key_qi_fw_path", str);
    }

    public static int getBattery(String str) {
        if (getDeviceManager().getDevice(str) == null || !(getDeviceManager().getDevice(str) instanceof AbsBleDevice)) {
            return 0;
        }
        return ((AbsBleDevice) getDeviceManager().getDevice(str)).getBattery();
    }

    public static String getFirmwareVersion(String str) {
        return (getDeviceManager().getDevice(str) == null || !(getDeviceManager().getDevice(str) instanceof AbsBleDevice)) ? "" : ((AbsBleDevice) getDeviceManager().getDevice(str)).getFirmwareVersion();
    }

    public static String getModel(String str) {
        return (getDeviceManager().getDevice(str) == null || !(getDeviceManager().getDevice(str) instanceof AbsBleDevice)) ? "" : ((AbsBleDevice) getDeviceManager().getDevice(str)).getModel();
    }

    public static void setDeviceFwVersion(String address, String value) {
        if (getDeviceManager().getDevice(address) == null || !(getDeviceManager().getDevice(address) instanceof AbsBleDevice)) {
            return ;
        }
        ((AbsBleDevice) getDeviceManager().getDevice(address)).setFirmwareVersion(value);
    }
}